package rbm.partida;
import java.awt.Component;
import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.util.List;


import rbm.modelado.datos.DatosPuzzle;
import rbm.modelado.datos.elementos.interfaces.Block;
import rbm.modelado.datos.elementos.interfaces.BoardShape;
import rbm.modelado.datos.elementos.interfaces.PuzzleFloor;
import rbm.modelado.global.interfaces.PuzzleConstants;


public class GameState implements PuzzleConstants
{

   private DatosPuzzle pd;
   private BoardShape board;
   private Block block;

   private Integer moveCount;

/*
   public GameState( DatosPuzzle p )
   {
     
      changePuzzle( p );
   }


   public void changePuzzle( DatosPuzzle p )
   {
      pd = p;
      if( pd == null )
      {
         board = null;
         block = null;
      }
      else
      {
         board = pd.getBoard();
         boolean[][][] blockShape = pd.getBlockShape();
         int[] startCoord = pd.getStartCoord();
         block = new BloqueImpl( blockShape, startCoord[0], startCoord[1], this );
         moveCount = 0;
      }
   }
*/
/*
   public void resetPuzzle()
   {
      if( pBank == null )
         return;

      changePuzzle( pBank.resetPuzzle() );
   }

   public void nextPuzzle()
   {
      if( pBank == null )
         return;

      changePuzzle( pBank.getNextPuzzle() );
   }

   public void prevPuzzle()
   {
      if( pBank == null )
         return;

      changePuzzle( pBank.getPrevPuzzle() );
   }
*/

   public List<PuzzleFloor> getPuzzle()
   {
      if( pd == null )
         return null;
      else
         return pd.getPuzzle();
   }
   
   public PuzzleFloor getFloor(Integer nivel)
   {
      if( pd == null )
         return null;
      else
         return pd.getFloor(nivel);
   }

/*

   public boolean getTile( int x, int y )
   {
      if( pd == null )
         return false;

      if( x < 0 || y < 0 || x >= board.length || y >= board[0].length )
         return true;
      else
         return board[x][y];
   }

   public int getHeight()
   {
      if( board == null )
         return 0;
      else
         return board[0].length;
   }

   public int getWidth()
   {
      if( board == null )
         return 0;
      else
         return board.length;
   }

   public int[] getFinishCoords()
   {
      if( pd == null )
         return null;
      else
         return pd.getFinishCoords();
   }

   public BloqueImpl getBlock()
   {
      if( block == null )
         return null;
      else
         return block;
   }

   public String getName()
   {
      if( pd == null )
         return null;
      else
         return pd.getName();
   }


   public int getMoveCount()
   {
      return moveCount;
   }


   public void roll( int dir )
   {
      if( block == null )
         return;

      if( block.roll( dir ) )
      {
         moveCount++;
         if( testComplete() )
            System.out.println( "Completed in " + moveCount + " moves" );
         
      }

   }




	// Go through each finish square to see if covered, then count
	// number of blocks in bottom layer of shape. If same as number
	// of finish squares, then completed

   private boolean testComplete()
   {

      int[] finish = pd.getFinishCoords();
      boolean[][] footprint = block.getLayer( 0 );
      int xPos = block.getXPos();
      int yPos = block.getYPos();

      for( int n = 0; n < finish.length; n += 2 )
      {
         if( finish[n] - xPos < 0 || finish[n] - xPos >= footprint.length ||
             finish[n+1] - yPos < 0 || finish[n+1] - yPos >= footprint[0].length )
                return false;

         if( !footprint[ finish[n] - xPos ][ finish[n+1] - yPos ] )
            return false;
      }

      int count = 0;

      for( int i = 0; i < footprint.length; i++ )
         for( int j = 0; j < footprint[i].length; j++ )
            if( footprint[i][j] )
               count++;

      if( 2 * count == finish.length )
         return true;
      else
         return false;
   }
*/
}